package fairygui.editor
{
   import fairygui.GButton;
   import fairygui.GComponent;
   import fairygui.GObject;
   import fairygui.GTextField;
   import fairygui.UIPackage;
   import fairygui.editor.extui.DropEvent;
   import fairygui.editor.extui.Icons;
   import fairygui.editor.gui.EGComponent;
   import fairygui.editor.gui.EGGroup;
   import fairygui.editor.gui.EGObject;
   import fairygui.event.DragEvent;
   import fairygui.event.GTouchEvent;
   import fairygui.event.ItemEvent;
   import fairygui.event.StateChangeEvent;
   import fairygui.tree.ITreeListener;
   import fairygui.tree.TreeNode;
   import fairygui.tree.TreeView;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.text.TextField;
   
   public class ChildrenPanel implements ITreeListener
   {
       
      
      public var self:GComponent;
      
      private var _editorWindow:EditorWindow;
      
      private var _treeView:TreeView;
      
      private var _treeItemUrl:String;
      
      private var _titleBar:GButton;
      
      private var _savedHeight:int;
      
      private var _findInput:GTextField;
      
      private var _updating:Boolean;
      
      private var _settingSelection:Boolean;
      
      public function ChildrenPanel(param1:EditorWindow, param2:GComponent)
      {
         super();
         this.self = param2;
         this._editorWindow = param1;
         this._treeView = new TreeView(this.self.getChild("list").asList);
         this._treeView.listener = this;
         this._treeItemUrl = UIPackage.getItemURL("Builder","ChildrenPanel_item");
         var _loc3_:GComponent = this.self.getChild("n22").asCom;
         _loc3_.getChild("status").addClickListener(this.__clickEyeAll);
         _loc3_.getChild("lock").addClickListener(this.__clickLockAll);
         _loc3_.getChild("expand").asButton.addEventListener(StateChangeEvent.CHANGED,this.__clickCollpaseAll);
         this._findInput = _loc3_.getChild("findInput").asTextField;
         this._findInput.addEventListener(KeyboardEvent.KEY_DOWN,this.__keyDown);
         _loc3_.getChild("btnFind").addClickListener(this.__clickFind);
         this.self.addEventListener(DropEvent.DROP,this.__drop);
         this._titleBar = this.self.getChild("titleBar").asButton;
         this._titleBar.addEventListener(StateChangeEvent.CHANGED,this.__expandStateChanged);
      }
      
      public function update(param1:EGObject) : void
      {
         var _loc2_:ComDocument = null;
         var _loc3_:EGComponent = null;
         var _loc4_:Object = null;
         var _loc5_:int = 0;
         var _loc6_:int = 0;
         var _loc7_:TreeNode = null;
         var _loc8_:Array = null;
         var _loc9_:GObject = null;
         this._updating = true;
         if(param1 == null)
         {
            this._treeView.root.removeChildren();
            _loc2_ = this._editorWindow.activeComDocument;
            if(_loc2_ == null)
            {
               return;
            }
            _loc3_ = _loc2_.editingContent;
            _loc4_ = {"toplevel":[this._treeView.root]};
            _loc5_ = _loc3_.numChildren;
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               param1 = _loc3_.getChildAt(_loc6_);
               if(param1 is EGGroup)
               {
                  _loc7_ = new TreeNode(true);
                  _loc7_.expanded = !EGGroup(param1).displayCollapsed;
                  _loc7_.data = param1;
                  _loc4_[param1.id] = [_loc7_];
               }
               _loc6_++;
            }
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               param1 = _loc3_.getChildAt(_loc6_);
               if(param1.groupInst == null)
               {
                  _loc4_.toplevel.push(param1);
               }
               else
               {
                  _loc8_ = _loc4_[param1.groupId];
                  if(_loc8_)
                  {
                     _loc8_.push(param1);
                  }
                  else
                  {
                     _loc4_.toplevel.push(param1);
                  }
               }
               _loc6_++;
            }
            this.addGroupChildren(_loc4_,"toplevel");
         }
         else
         {
            _loc5_ = this._treeView.list.numChildren;
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               _loc9_ = this._treeView.list.getChildAt(_loc6_);
               if(_loc9_.name == param1.id)
               {
                  this._treeView.updateNode(TreeNode(_loc9_.data));
                  break;
               }
               _loc6_++;
            }
         }
         this._updating = false;
      }
      
      public function syncSelection() : void
      {
         var _loc5_:GObject = null;
         var _loc6_:TreeNode = null;
         var _loc7_:EGObject = null;
         if(this._settingSelection)
         {
            return;
         }
         this._settingSelection = true;
         var _loc1_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc1_ == null)
         {
            return;
         }
         var _loc2_:Vector.<EGObject> = _loc1_.getSelections();
         this._treeView.clearSelection();
         var _loc3_:int = this._treeView.list.numChildren;
         var _loc4_:int = 0;
         while(_loc4_ < _loc3_)
         {
            _loc5_ = this._treeView.list.getChildAt(_loc4_);
            _loc6_ = TreeNode(_loc5_.data);
            _loc7_ = EGObject(_loc6_.data);
            if(_loc2_.indexOf(_loc7_) != -1)
            {
               this._treeView.addSelection(_loc6_,true);
            }
            else
            {
               this._treeView.removeSelection(_loc6_);
            }
            _loc4_++;
         }
         this._settingSelection = false;
      }
      
      private function addGroupChildren(param1:Object, param2:String) : void
      {
         var _loc7_:EGObject = null;
         var _loc8_:TreeNode = null;
         var _loc3_:Array = param1[param2];
         var _loc4_:int = _loc3_.length;
         var _loc5_:TreeNode = _loc3_[0];
         var _loc6_:int = 1;
         while(_loc6_ < _loc4_)
         {
            _loc7_ = _loc3_[_loc6_];
            if(!(_loc7_ is EGGroup))
            {
               _loc8_ = new TreeNode(false);
               _loc8_.data = _loc7_;
               _loc5_.addChild(_loc8_);
            }
            else
            {
               _loc5_.addChild(param1[_loc7_.id][0]);
               this.addGroupChildren(param1,_loc7_.id);
            }
            _loc6_++;
         }
      }
      
      private function findNode(param1:TreeNode, param2:EGObject) : TreeNode
      {
         var _loc5_:TreeNode = null;
         var _loc6_:TreeNode = null;
         var _loc3_:int = param1.numChildren;
         var _loc4_:int = 0;
         while(_loc4_ < _loc3_)
         {
            _loc5_ = param1.getChildAt(_loc4_);
            if(_loc5_.data == param2)
            {
               return _loc5_;
            }
            if(_loc5_.isFolder)
            {
               _loc6_ = this.findNode(_loc5_,param2);
               if(_loc6_)
               {
                  return _loc6_;
               }
            }
            _loc4_++;
         }
         return null;
      }
      
      public function treeNodeCreateCell(param1:TreeNode) : GComponent
      {
         var _loc2_:GButton = this._treeView.list.getFromPool(this._treeItemUrl).asButton;
         _loc2_.draggable = true;
         _loc2_.addEventListener(DragEvent.DRAG_START,this.__dragNodeStart);
         var _loc3_:GButton = _loc2_.getChild("status").asButton;
         _loc3_.changeStateOnClick = false;
         _loc3_.addEventListener(GTouchEvent.BEGIN,this.__statusMouseDown);
         var _loc4_:GButton = _loc2_.getChild("lock").asButton;
         _loc4_.changeStateOnClick = false;
         _loc4_.addEventListener(GTouchEvent.BEGIN,this.__lockMouseDown);
         return _loc2_;
      }
      
      public function treeNodeRender(param1:TreeNode, param2:GComponent) : void
      {
         var _loc3_:GButton = param2.asButton;
         _loc3_.name = param1.data.id;
         _loc3_.title = param1.data.toString();
         _loc3_.icon = Icons.all[param1.data.objectType];
         _loc3_.data = param1;
         var _loc4_:GButton = _loc3_.getChild("status").asButton;
         _loc4_.selected = param1.data.hideByEditor;
         var _loc5_:GButton = _loc3_.getChild("lock").asButton;
         _loc5_.selected = param1.data.locked;
      }
      
      public function treeNodeWillExpand(param1:TreeNode, param2:Boolean) : void
      {
         var _loc4_:ComDocument = null;
         if(this._updating)
         {
            return;
         }
         var _loc3_:EGGroup = param1.data as EGGroup;
         if(_loc3_ != null)
         {
            _loc3_.displayCollapsed = !_loc3_.displayCollapsed;
            _loc4_ = this._editorWindow.activeComDocument;
            if(_loc4_ != null)
            {
               _loc4_.setModified();
            }
         }
      }
      
      public function treeNodeClick(param1:TreeNode, param2:ItemEvent) : void
      {
         var _loc5_:Vector.<TreeNode> = null;
         var _loc6_:EGObject = null;
         var _loc3_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc3_ == null)
         {
            return;
         }
         var _loc4_:EGObject = EGObject(param1.data);
         if(_loc4_ != null && _loc4_.parent != null)
         {
            this._settingSelection = true;
            _loc5_ = this._treeView.getSelection();
            if(_loc5_.length == 1 || !(this._editorWindow.groot.shiftKeyDown || this._editorWindow.groot.ctrlKeyDown) && !param2.rightButton)
            {
               _loc3_.openAndSetSelection(_loc4_);
            }
            else
            {
               if(this._editorWindow.groot.ctrlKeyDown)
               {
                  _loc3_.removeSelection(_loc4_);
               }
               for each(param1 in _loc5_)
               {
                  _loc6_ = EGObject(param1.data);
                  if(_loc6_.groupInst == _loc3_.openedGroup)
                  {
                     if(!_loc3_.inSelection(_loc6_))
                     {
                        _loc3_.addSelection(_loc6_);
                     }
                  }
                  else
                  {
                     this._treeView.list.removeSelection(this._treeView.getNodeIndex(param1));
                  }
               }
            }
            this._settingSelection = false;
            if(param2)
            {
               if(param2.rightButton)
               {
                  _loc3_.showContextMenu();
               }
               else if(param2.clickCount == 2)
               {
                  _loc3_.openChild(_loc4_);
               }
            }
         }
      }
      
      private function __dragNodeStart(param1:DragEvent) : void
      {
         var _loc5_:TreeNode = null;
         var _loc6_:EGObject = null;
         param1.preventDefault();
         var _loc2_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc2_ == null)
         {
            return;
         }
         if(_loc2_.isSelectingObject)
         {
            return;
         }
         var _loc3_:GButton = GButton(param1.currentTarget);
         if(!_loc3_.selected)
         {
            this._treeView.clearSelection();
            _loc5_ = TreeNode(_loc3_.data);
            _loc6_ = EGObject(_loc5_.data);
            this._treeView.addSelection(_loc5_);
            _loc2_.openAndSetSelection(_loc6_);
         }
         else
         {
            _loc5_ = this._treeView.getSelectedNode();
            _loc6_ = EGObject(_loc5_.data);
            if(!_loc2_.inSelection(_loc6_))
            {
               this._settingSelection = true;
               if(_loc6_.groupInst != _loc2_.openedGroup)
               {
                  _loc2_.openAndSetSelection(_loc6_);
               }
               else
               {
                  _loc2_.addSelection(_loc6_);
               }
               this._settingSelection = false;
               this.syncSelection();
            }
         }
         var _loc4_:Vector.<TreeNode> = this._treeView.getSelection();
         this._editorWindow.dragManager.startDrag(this,_loc4_);
      }
      
      private function __added(param1:Event) : void
      {
         this.update(null);
      }
      
      private function __statusMouseDown(param1:Event) : void
      {
         param1.stopPropagation();
         var _loc2_:GButton = GButton(param1.currentTarget.parent);
         var _loc3_:GButton = _loc2_.getChild("status").asButton;
         _loc3_.selected = !_loc3_.selected;
         var _loc4_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc4_ == null)
         {
            return;
         }
         var _loc5_:EGComponent = _loc4_.editingContent;
         var _loc6_:EGObject = _loc5_.getChildById(_loc2_.name);
         if(_loc6_ != null)
         {
            _loc6_.hideByEditor = _loc3_.selected;
            _loc4_.setModified();
         }
         _loc5_.updateDisplayList();
      }
      
      private function __lockMouseDown(param1:Event) : void
      {
         param1.stopPropagation();
         var _loc2_:GButton = GButton(param1.currentTarget.parent);
         var _loc3_:GButton = _loc2_.getChild("lock").asButton;
         _loc3_.selected = !_loc3_.selected;
         var _loc4_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc4_ == null)
         {
            return;
         }
         var _loc5_:EGComponent = _loc4_.editingContent;
         var _loc6_:EGObject = _loc5_.getChildById(_loc2_.name);
         if(_loc6_ != null)
         {
            _loc6_.locked = _loc3_.selected;
            _loc4_.setModified();
         }
      }
      
      private function __clickEyeAll(param1:Event) : void
      {
         var _loc7_:EGObject = null;
         var _loc8_:GButton = null;
         var _loc9_:GButton = null;
         var _loc2_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc2_ == null)
         {
            return;
         }
         var _loc3_:EGComponent = _loc2_.editingContent;
         var _loc4_:Boolean = false;
         var _loc5_:int = _loc3_.numChildren;
         var _loc6_:int = 0;
         while(_loc6_ < _loc5_)
         {
            _loc7_ = _loc3_.getChildAt(_loc6_);
            if(_loc7_.hideByEditor)
            {
               _loc7_.hideByEditor = false;
               _loc4_ = true;
            }
            _loc6_++;
         }
         if(_loc4_)
         {
            _loc5_ = this._treeView.list.numChildren;
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               _loc8_ = this._treeView.list.getChildAt(_loc6_).asButton;
               _loc9_ = _loc8_.getChild("status").asButton;
               _loc9_.selected = false;
               _loc6_++;
            }
            _loc2_.setModified();
            _loc3_.updateDisplayList();
         }
      }
      
      private function __clickLockAll(param1:Event) : void
      {
         var _loc7_:EGObject = null;
         var _loc8_:GButton = null;
         var _loc9_:GButton = null;
         var _loc2_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc2_ == null)
         {
            return;
         }
         var _loc3_:EGComponent = _loc2_.editingContent;
         var _loc4_:Boolean = false;
         var _loc5_:int = _loc3_.numChildren;
         var _loc6_:int = 0;
         while(_loc6_ < _loc5_)
         {
            _loc7_ = _loc3_.getChildAt(_loc6_);
            if(_loc7_.locked)
            {
               _loc7_.locked = false;
               _loc4_ = true;
            }
            _loc6_++;
         }
         if(_loc4_)
         {
            _loc5_ = this._treeView.list.numChildren;
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               _loc8_ = this._treeView.list.getChildAt(_loc6_).asButton;
               _loc9_ = _loc8_.getChild("lock").asButton;
               _loc9_.selected = false;
               _loc6_++;
            }
            _loc2_.setModified();
         }
      }
      
      private function __clickCollpaseAll(param1:Event) : void
      {
         var _loc8_:EGObject = null;
         var _loc2_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc2_ == null)
         {
            return;
         }
         var _loc3_:EGComponent = _loc2_.editingContent;
         var _loc4_:Boolean = false;
         var _loc5_:Boolean = param1.currentTarget.selected;
         var _loc6_:int = _loc3_.numChildren;
         var _loc7_:int = 0;
         while(_loc7_ < _loc6_)
         {
            _loc8_ = _loc3_.getChildAt(_loc7_);
            if(_loc8_ is EGGroup)
            {
               if(EGGroup(_loc8_).displayCollapsed != _loc5_)
               {
                  EGGroup(_loc8_).displayCollapsed = _loc5_;
                  _loc4_ = true;
               }
            }
            _loc7_++;
         }
         if(_loc4_)
         {
            this._updating = true;
            if(_loc5_)
            {
               this._treeView.collapseAll(this._treeView.root);
            }
            else
            {
               this._treeView.expandAll(this._treeView.root);
            }
            this._updating = false;
            _loc2_.setModified();
         }
      }
      
      private function __drop(param1:DropEvent) : void
      {
         var _loc5_:EGObject = null;
         if(!(param1.source is ChildrenPanel))
         {
            return;
         }
         var _loc2_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc2_ == null)
         {
            return;
         }
         if(_loc2_.editingTransition != null)
         {
            return;
         }
         var _loc3_:EGComponent = _loc2_.editingContent;
         var _loc4_:GObject = this._treeView.list.getItemNear(this.self.displayObject.stage.mouseX,this.self.displayObject.stage.mouseY);
         if(_loc4_ == null)
         {
            _loc5_ = _loc3_.getChildAt(_loc3_.numChildren - 1);
         }
         else
         {
            _loc5_ = _loc3_.getChildById(_loc4_.name);
         }
         if(_loc5_ != null)
         {
            _loc2_.moveCrossGroups(_loc5_);
         }
      }
      
      private function __expandStateChanged(param1:Event) : void
      {
         if(this._titleBar.selected)
         {
            this._savedHeight = this.self.height;
            this.self.height = this._titleBar.height;
            this.self.y = this.self.y + this._savedHeight - this._titleBar.height;
            this._editorWindow.mainPanel.libPanelResizerV.visible = false;
         }
         else
         {
            this.self.height = this._savedHeight;
            this.self.y = this.self.y - (this.self.height - this._titleBar.height);
            this._editorWindow.mainPanel.libPanelResizerV.visible = true;
         }
      }
      
      public function getCorrectResizerPos() : int
      {
         if(this._titleBar.selected)
         {
            return this._editorWindow.mainPanel.libPanelResizerV.y - (this._savedHeight - this._titleBar.height);
         }
         return this._editorWindow.mainPanel.libPanelResizerV.y;
      }
      
      private function __clickFind(param1:Event) : void
      {
         var _loc7_:EGObject = null;
         var _loc8_:TreeNode = null;
         var _loc2_:String = this._findInput.text.toLowerCase();
         if(_loc2_.length == 0)
         {
            this._editorWindow.stage.focus = this._findInput.displayObject as TextField;
            return;
         }
         var _loc3_:ComDocument = this._editorWindow.activeComDocument;
         if(_loc3_ == null)
         {
            return;
         }
         var _loc4_:EGComponent = _loc3_.editingContent;
         var _loc5_:int = _loc4_.numChildren;
         var _loc6_:int = 0;
         while(_loc6_ < _loc5_)
         {
            _loc7_ = _loc4_.getChildAt(_loc6_);
            if(_loc7_.name.toLowerCase().indexOf(_loc2_) != -1)
            {
               _loc8_ = this.findNode(this._treeView.root,_loc7_);
               if(_loc8_)
               {
                  this._treeView.addSelection(_loc8_,true);
               }
               _loc3_.openAndSetSelection(_loc7_);
               return;
            }
            _loc6_++;
         }
      }
      
      private function __keyDown(param1:KeyboardEvent) : void
      {
         if(param1.keyCode == 13)
         {
            this.__clickFind(null);
         }
      }
   }
}
